Reading/Writing JSON files

JSON documents are very popular file format and you may always see them when getting a response from an API. They are very similar to Python dictionaries in a sense that both of them are pairs of keys and values. Because of their popularity, a standing out Python package called JSON is coming preinstalled once you download and install Python. THe package provdes some nice functions for dealing with JSON documents. This notebook provides introduction on reading JSON files from a string variable, local file and from a URL, as well as points the necessary functions to write a variable into an existing or a new JSON file.

Note: Only Parsing JSON from a string variable part is posted now, as the rest was not covered during the lecture.

Parsing JSON from a string variable


In [1]:
import json

The JSON package has a loads() function which takes a string as an argument and converts it to JSON. THus, let's a create a string which will include a JSON file. In our case this file will be a list of 2 dictionaries, where each of the dictionaires will have 3 keys and a value for each as presented below.


In [2]:
input = '''[
  { "id" : "01",
    "status" : "Instructor",
    "name" : "Hrant"
  } ,
  { "id" : "02",
    "status" : "Student",
    "name" : "Jack"
  }
]'''

Once we have the string, we can use the abovementioned loads() function from the JSON package to convert the string variable into JSON. Please note, that there is another similar function called load() (without s), which does the same but for local files, rather than a string variable inside Python. Thus, as we have a string variable to load, the correct function will be loads().


In [3]:
data = json.loads(input)

Let's check the type of the data variable.


In [4]:
type(data)


Out[4]:
list

As expected, data is a list, a list that have 2 dictionaire elements.

Let's count how many users we have information on in our list (i.e. we have 2 dictionaires so basically we already know the answer).


In [5]:
print 'User count:', len(data)


User count: 2

Let's print the data to see those 2 elements


In [6]:
print(data)


[{u'status': u'Instructor', u'id': u'01', u'name': u'Hrant'}, {u'status': u'Student', u'id': u'02', u'name': u'Jack'}]

We can even pretty print it, to make the output more user friendly.


In [7]:
from pprint import pprint

In [8]:
pprint(data)


[{u'id': u'01', u'name': u'Hrant', u'status': u'Instructor'},
 {u'id': u'02', u'name': u'Jack', u'status': u'Student'}]

Fine, we see the two elements. Now, let's print the content (values) by choosing them by keys. For that purpose, we must create a for loop, that will iterate over the elements in the list (2 dictionaires, in our case) and for each of the elements print some text (e.g. the key, which are "Name", "ID" and "Status") followed by the value of that key.


In [9]:
for item in data:
    print 'Name: ', item['name']
    print 'Id: ', item['id']
    print 'Status: ', item['status'], "\n"


Name:  Hrant
Id:  01
Status:  Instructor 

Name:  Jack
Id:  02
Status:  Student 

Excellent! The ourput is much more readible in this case. Also, please take a look at the "\n" in the loop: it exists in order to separate the output of two elements with a new line, as presented in the output above.